import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import router from '../../router'
const http = axios.create({
    baseURL: '/api',
    timeout: 120000
})


/****** request拦截器==>对请求参数做处理 ******/
http.interceptors.request.use(config => {
    if(localStorage.getItem('token')) {
        const token = localStorage.getItem('token')
        config.headers['x-access-token'] = token
    }
    return config;
}, error => {  //请求错误处理
    Promise.reject(error)
});

/****** respone拦截器==>对响应做处理 ******/
http.interceptors.response.use(
    response => {  //成功请求到数据
        const { data: res, headers } = response
        if(headers?.['x-access-token']) {
            const token = headers['x-access-token']
            localStorage.setItem('token',token)
        }
        if(res.code === 200) {
            Message({
                message: res.message,
                type: 'success'
            })
        } else if (res.code === 500) {
            Message({
                message: res.message,
                type: 'error'
            })
        }
        return res
    },
    error => {  //响应错误处理
        console.log(error, 'error');
        return Promise.reject(error)
    }
);
export default http